<template>
  <div class="container">
    <el-card style="max-width: 580px;margin: 30px auto;">
      <template #header>
        <div class="card-header">
          <span>重置密码</span>
        </div>
      </template>
      <el-form size="large" :rules="rules" :model="pwds" ref="pwdsRef" label-position="right"
        label-width="100">
        <el-form-item label="登录名称">
          <el-input style="font-size: 18px;" disabled :value="userInfo.username" />
        </el-form-item>
        <el-form-item label="原密码" prop="old_pwd">
          <el-input style="font-size: 18px;" v-model="pwds.old_pwd"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="new_pwd">
          <el-input style="font-size: 18px;" v-model="pwds.new_pwd"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="re_pwd">
          <el-input style="font-size: 18px;" v-model="pwds.re_pwd"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updatePwdsInfo">提交修改</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script setup>
import { userUpdatePwdApi } from '@/api/user';
import { useUserStore } from '@/stores/useUserStore';
import { ElMessage } from 'element-plus';
import { storeToRefs } from 'pinia';
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter()
const userStore = useUserStore()

const { userInfo } = storeToRefs(userStore)

const pwds = reactive({
  old_pwd: '',
  new_pwd: '',
  re_pwd: ''
})


const rules = {
  old_pwd: [
    { required: true, message: '请输入原密码', trigger: 'blur' }
  ],
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
  ],
  re_pwd:[
    {required:true,message:'请再次确认密码',trigger:'blur'}
  ]
}
const pwdsRef = ref()

const updatePwdsInfo = async () => {
  await pwdsRef.value.validate()
  console.log('表单校验通过')
  await userUpdatePwdApi(pwds)
  ElMessage.success('修改密码成功')
  // 密码修改之后token会更新，所以得重新登录
  router.replace('/login')
}
</script>

<style scoped lang="scss"></style>